#include <iostream>
#include <unistd.h>
#include <pthread.h>

using namespace std;
int val_ = 20000;

void* pthreadRun(void* arg){
    while(true){
        if(val_>0){
            //开始抢票
            val_--;
            cout<<"线程："<<(char*)arg<<"抢票成功，剩余："<<val_<<"张票"<<endl;
        }else{
            break;
        }
        usleep(1000);
    }
    return nullptr;
}
const int num = 5;
int main()
{
    pthread_t pths[num];
    for(int i = 1;i<=num;++i){
        char* name = new char[64];
        snprintf(name,64,"thread-%d",i);
        pthread_create(&pths[i-1],nullptr,pthreadRun,name);
    }
    for(int i = 1;i<=num;++i){
        pthread_join(pths[i-1],nullptr);
    }
    cout<<"剩余票数:"<<val_<<endl;
    return 0;
}